package treeedit;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PathChanger extends JFrame implements ActionListener
{
	
	JTextField field = new JTextField();
		
	public PathChanger ( )
	{
		setLayout(new FlowLayout());
		JButton but ;
		JLabel info = new JLabel();
		info.setText("Specify path to clisp executable:");
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout (1,2));
		
		field.setPreferredSize(new Dimension(150,field.getPreferredSize().height));
		field.addActionListener(this);
		field.setActionCommand("ok");
		
		but = new JButton("Ok");
		but.setActionCommand("ok");
		but.addActionListener(this);
		
		panel.add(but);
		
		but = new JButton("Cancel");
		but.setActionCommand("cancel");
		but.addActionListener(this);
		
		panel.add(but);
		
		but = new JButton("Browse");
		but.setActionCommand("browse");
		but.addActionListener(this);
		
		panel.add(but);
		
		
		add(info);
		add(field);
		add(panel);
		
		field.setText(Configuration.getInstance().getClispPath());
		
		setPreferredSize(new Dimension(250,110));
		
		pack();
		
		setResizable(false);
		setVisible(true);
		
	}

	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();
		
		if(action.equals("ok"))
		{
			Configuration.getInstance().setLispPath(field.getText());
			//Configuration.getInstance().store();
		}
		else if(action.equals("browse"))
		{
			JFileChooser chooser = new JFileChooser();
			
			int choice = chooser.showDialog(this, "Select");
			
			if(choice == JFileChooser.APPROVE_OPTION)
			{
				field.setText(chooser.getSelectedFile().getAbsolutePath());
			}
			return ;
		}
		setVisible(false);
		dispose();
				
	}
}
